﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SunTzu.Core.Data;
using System.Data.Linq;

namespace Odyssey.Linq
{
    /// <summary>
    /// 数据对象扩展
    /// </summary>
    public static class DataExtension
    {
        /// <summary>
        /// 保存或更新
        /// </summary>
        /// <param name="data">要保存或更新的数据</param>
        /// <returns>操作结果</returns>
        public static bool SaveOrUpdate(this IEntity data)
        {
            bool success = false;
            if (data != null)
            {
                var db = DataContextManager.GetContext();

                if (data.Id < 1)
                {
                    data.CreateDate = DateTime.Now;
                    data.UpdateDate = DateTime.Now;
                    db.GetTable(data.GetType()).InsertOnSubmit(data);
                }
                else
                {
                    //data.UpdateDate = DateTime.Now;
                }

                try
                {
                    db.SubmitChanges();
                    success = true;
                }
                catch(Exception e)
                {
                    success = false;
                    throw e;
                }
            }
            return success;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="data">要删除的数据</param>
        /// <param name="isLogicDelete">是否逻辑删除</param>
        /// <returns>操作结果</returns>
        public static bool Delete(this IEntity data, bool isLogicDelete)
        {
            bool success = false;
            var db = DataContextManager.GetContext();
            if (isLogicDelete)
            {
                db.GetTable(data.GetType()).DeleteOnSubmit(data);
            }
            else
            {
                // data.State = -1; // state = -1 时，逻辑删除
            }
            try
            {
                db.SubmitChanges();
                success = true;
            }
            catch (Exception)
            {
                success = false;
            }
            return success;
        }

        /// <summary>
        /// 逻辑删除数据
        /// </summary>
        /// <param name="data">要删除的数据</param>
        /// <returns>操作结果</returns>
        public static bool Delete(this IEntity data)
        {
            return Delete(data, true);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="dataList">要删除的数据</param>
        /// <param name="isLogicDelete">是否逻辑删除</param>
        /// <returns>操作结果</returns>
        public static bool Delete(this List<IEntity> dataList, bool isLogicDelete)
        {
            bool success = false;
            if (dataList != null && dataList.Count > 0)
            {
                var db = DataContextManager.GetContext();
                if (isLogicDelete)
                {
                    // dataList.ForEach(c => c.State = -1);
                }
                else
                {
                    db.GetTable(dataList[0].GetType()).DeleteAllOnSubmit(dataList);
                }
                try
                {
                    db.SubmitChanges();
                    success = true;
                }
                catch (Exception)
                {
                    success = false;
                }
            }
            return success;
        }

        /// <summary>
        /// 逻辑删除数据
        /// </summary>
        /// <param name="dataList">要删除的数据</param>
        /// <returns>操作结果</returns>
        public static bool Delete(this List<IEntity> dataList)
        {
            return Delete(dataList, true);
        }

        public static string GetTitle(this ICulture entity, string culture)
        {
            if (string.IsNullOrEmpty(culture))
            {
                return entity.zh_cn;
            }

            if ("zh_cn".Equals(culture, StringComparison.OrdinalIgnoreCase))
            {
                return entity.zh_cn;
            }

            if ("zh_tw".Equals(culture, StringComparison.OrdinalIgnoreCase))
            {
                if (string.IsNullOrEmpty(entity.zh_tw))
                { return entity.zh_cn; }
                return entity.zh_tw;
            }

            if (string.IsNullOrEmpty(entity.en_us))
            { return entity.zh_cn; }
            return entity.en_us;
        }
    }
}
